package sample.algo.sistr.algosample.algos.graphs;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.Iterator;

/**
 * Created by sistr on 28/09/14.
 */
public class GraphView extends View {

    private Graph graph;

    private static Paint nodePaint;
    private static Paint nodeOutline;
    private static Paint nodeText;

    private static Paint edgePaint;
    private static Paint edgeText;

    static {
        nodePaint = new Paint();
        nodePaint.setColor(Color.argb(255, 137, 182, 255));
        nodePaint.setStyle(Paint.Style.FILL);
        nodePaint.setStrokeWidth(5.0f);

        nodeOutline = new Paint();
        nodeOutline.setColor(Color.BLACK);
        nodeOutline.setStyle(Paint.Style.STROKE);
        nodeOutline.setStrokeWidth(5.0f);

        nodeText = new Paint();
        nodeText.setColor(Color.BLACK);
        nodeText.setStyle(Paint.Style.FILL);
        nodeText.setStrokeWidth(3.0f);
        nodeText.setTextSize(40.0f);

        edgePaint = new Paint();
        edgePaint.setColor(Color.BLACK);
        edgePaint.setStrokeWidth(5.0f);

        edgeText = new Paint();
        edgeText.setColor(Color.BLACK);
        edgeText.setStyle(Paint.Style.FILL);
        edgeText.setStrokeWidth(3.0f);
        edgeText.setTextSize(40.0f);
    }

    public GraphView(Context context, AttributeSet attrs) {
        super(context, attrs);
        graph = null;
    }

    public void SetGraph(Graph graph) {
        this.graph = graph;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = canvas.getWidth();
        float height = canvas.getHeight();

        if(graph != null) {
            Iterator<Node> nodeIt = graph.start();

            // First draw all the edges
            while(nodeIt.hasNext()) {
                Node n = nodeIt.next();

                for(Edge e : n.out_edges) {
                    canvas.drawLine(e.start.x*width, e.start.y*height, e.end.x*width, e.end.y*height, edgePaint);
                }
            }

            // Second draw all the nodes
            nodeIt = graph.start();
            while(nodeIt.hasNext()) {
                Node n = nodeIt.next();

                if(n.state == Node.State.NORMAL) {
                    nodeOutline.setColor(Color.BLACK);
                    canvas.drawCircle(width * n.x, height * n.y, 40.0f, nodePaint);
                    canvas.drawCircle(width * n.x, height * n.y, 40.0f, nodeOutline);
                } else {
                    nodeOutline.setColor(Color.GREEN);
                    canvas.drawCircle(width * n.x, height * n.y, 40.0f, nodePaint);
                    canvas.drawCircle(width * n.x, height * n.y, 40.0f, nodeOutline);
                }

                Rect bounds = new Rect();
                nodeText.getTextBounds("" + n.id, 0, 1, bounds);
                nodeText.setColor(Color.BLACK);
                canvas.drawText("" + n.id, width * n.x - bounds.width()/2.0f, (height * n.y) + bounds.height()/2.0f, nodeText);

                if(n.reaching_cost >= -1) {
                    nodeText.setColor(Color.RED);
                    if(n.reaching_cost == -1) {
                        canvas.drawText("∞", width * n.x - bounds.width() / 2.0f, (height * n.y) + bounds.height() * 1.5f, nodeText);
                    } else {
                        canvas.drawText("" + n.reaching_cost, width * n.x - bounds.width() / 2.0f, (height * n.y) + bounds.height() * 1.5f, nodeText);
                    }
                }

            }
        }
    }
}
